package com.ithinks.flow.common.utils.middleware.rabbitMQ.pubsubmode;

import com.ithinks.flow.common.utils.middleware.rabbitMQ.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description
 * @ClassName Sms_Consumer
 * @Author MR.LI
 * @date 2020.11.27 15:10
 */
public class Sms_Consumer {

    private static final String EXCHANGE_FANOUT_INFORM = "exchange_fanout_inform";

    private static final String QUEUE_INFORM_SMS = "queue_inform_sms";

    private static String IP = "localhost";
    //端口
    private static int PORT = 5672;
    //虚拟机IP，默认/
    private static String VHOST = "/";
    //队列名称
    private static String USERNAME = "guest";

    private static String PASSWORD = "guest";

    public static void main(String[] args)  {
        //1、创建连接
        Connection connection = ConnectionUtils.getConnection(IP, PORT, VHOST, USERNAME, PASSWORD);

        //2、创建通道
        Channel channel = null;
        try {
            channel = connection.createChannel();
            channel.exchangeDeclare(EXCHANGE_FANOUT_INFORM, BuiltinExchangeType.FANOUT);

            channel.queueDeclare(QUEUE_INFORM_SMS,true,false,false,null);

            channel.queueBind(QUEUE_INFORM_SMS,EXCHANGE_FANOUT_INFORM,"");

            DefaultConsumer consumer = new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    super.handleDelivery(consumerTag, envelope, properties, body);
                    long deliveryTag = envelope.getDeliveryTag();
                    String exchange = envelope.getExchange();
                    String message = new String(body,"UTF-8");
                    System.out.println(message);
                }
            };
            channel.basicConsume(QUEUE_INFORM_SMS,true,consumer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
